<?php

namespace app\controller;

use app\BaseController;
use app\middleware\CheckToken;
use app\model\Student;
use app\model\Teacher;
use app\model\VStudentInfo;
use app\model\VTeacherInfo;
use app\services\JwtService;
use think\request;
use \think\facade\Db;

class UserInfo extends BaseController
{

    private $managerPermisson = [
        [
            'label' => '管理员',
            'value' => 'manager',
        ],[
            'label' => '基础列表',
            'value' => 'basic_list',
        ],[
            'label' => '基础列表删除',
            'value' => 'basic_list_delete',
        ]
    ];

    private $teacherPermisson = [
        [
            'label' => '教师',
            'value' => 'teacher',
        ],[
            'label' => '基础列表',
            'value' => 'basic_list',
        ],[
            'label' => '基础列表删除',
            'value' => 'basic_list_delete',
        ]
    ];

    private $studentPermisson = [
        [
            'label' => '学生',
            'value' => 'student',
        ],[
            'label' => '基础列表',
            'value' => 'basic_list',
        ],[
            'label' => '基础列表删除',
            'value' => 'basic_list_delete',
        ]
    ];

    public function index()
    {
        $user = $this->user;

        $permisson = [];

        if($user['desc'] == 'manager'){
            $permisson = $this->managerPermisson;
        }else if($user['desc'] == 'teacher'){
            $permisson = $this->teacherPermisson;
        }else{
            $permisson = $this->studentPermisson;
        }

        return \resultSuccess(array(
            'token' => $this->request->middleware('token'),
            'userId' => $this->user['userId'],
            'username' => $this->user['username'],
            'realname' => $this->user['realname'],
            'desc' => $this->user['desc'],
            'permissions' => $permisson,
        ));
    }

    public function getMyInfo(){
        if($this->user['desc']=='student'){
            $info = VStudentInfo::where(['SID'=>$this->user['SID']])->find();
            return resultSuccess($info);
        }else{
            $info = VTeacherInfo::where(['TID'=>$this->user['TID']])->find();
            return resultSuccess($info);
        }
    }

    public function changePassword(){
        $oldPassword = $this->request->param('oldPassword');
        $newPassword = $this->request->param('newPassword');
        $repeatPassword = $this->request->param('repeatPassword');
        if($oldPassword == null || $newPassword==null || $repeatPassword==null){
            return \resultError('表单验证失败');
        }
        if($newPassword != $repeatPassword){
            return resultError('新密码不一致');
        }
        if($this->user['desc']=='student'){
            $info = Student::where(['SID'=>$this->user['SID']])->find();
            if(password_verify($oldPassword,$info->password) == false){
                return resultError('旧密码不正确');
            }
            if($oldPassword == $newPassword){
                return resultError('新旧密码不能相同');
            }
            $info->password=password_hash($newPassword,PASSWORD_DEFAULT);
            if($info->save()){
                return resultSuccess();
            }else{
                return resultError('保存失败');
            }

        }else{
            $info = Teacher::where(['TID'=>$this->user['TID']])->find();
            if(!password_verify($oldPassword,$info->password)){
                return resultError('旧密码不正确');
            }
            if($oldPassword == $newPassword){
                return resultError('新旧密码不能相同');
            }
            $info->password=password_hash($newPassword,PASSWORD_DEFAULT);
            if($info->save()){
                return resultSuccess();
            }else{
                return resultError('保存失败');
            }
        }
    }

    public function resetPassword(){

    }
}
